import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class CalculatePowerConfigService {
  constructor(private prisma: PrismaService) {}

  async calculate(config: Record<string, any>) {
    return await this.prisma.calcPowerConfig.create({
      data: {
        zoneId: +config.zoneId,
        QianWireId: +config.QianWireId,
        DaoWireId: +config.DaoWireId,
        e: +config.e,
        num: +config.num,
        order: config.order === 'true',
        isCalc: false,
        isSmallQianZhang: config.isSmallQianZhang === 'true',
      },
    });
  }

  async updadte(data: any, id: number) {
    return await this.prisma.calcPowerConfig.update({ where: { id }, data });
  }

  async find(id: number) {
    return await this.prisma.calcPowerConfig.findFirst({
      where: { id },
      include: {
        DaoWire: true,
        QianWire: true,
      },
    });
  }

  async findAll(args: Record<string, any>) {
    const { zoneId } = args;
    return await this.prisma.calcPowerConfig.findMany({
      where: { zoneId: +zoneId },
    });
  }
}
